package com.fighYo.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fighYo.commonutils.R;
import com.fighYo.edu.entity.EduCourse;
import com.fighYo.edu.entity.vo.CourseInfoVo;
import com.fighYo.edu.entity.vo.CoursePublishVo;
import com.fighYo.edu.entity.vo.CourseQuery;
import com.fighYo.edu.service.EduCourseService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2021-08-24
 */
@CrossOrigin
@RestController
@RequestMapping("/edu/course")
public class EduCourseController {

    @Autowired
    private EduCourseService eduCourseService;

    @ApiModelProperty("增加课程")
    @PostMapping("addCourse")
    public R addCourse(@RequestBody CourseInfoVo courseInfo) {
        String id = eduCourseService.saveCourseInfo(courseInfo);
        return R.ok().data("courseId", id);
    }

    @PutMapping("updateCourse")
    public R updateCourse(@RequestBody CourseInfoVo courseInfo) {
        eduCourseService.updateCourseInfo(courseInfo);
        return R.ok();
    }

    @GetMapping("getCourse/{id}")
    public R getCourseById(@PathVariable("id") String id) {
        CourseInfoVo courseInfoVo = eduCourseService.getCourseInfo(id);
        return R.ok().data("item", courseInfoVo);
    }

    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable("id") String id) {
        CoursePublishVo coursePublishVo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("item", coursePublishVo);
    }

    @PutMapping("publishCourse/{id}")
    public R publishCourseById(@PathVariable("id") String id) {
        eduCourseService.publishCourseById(id);
        return R.ok();
    }

    @GetMapping("getPageList/{cur}/{limit}")
    public R getCourseByCondition(@PathVariable("cur") Long cur,
                                  @PathVariable("limit") Long limit,
                                  CourseQuery courseQuery) {
        Page<EduCourse> page = new Page<>(cur, limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseQuery.getTitle())) {
            wrapper.like("title", courseQuery.getTitle());
        }
        if(!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            wrapper.eq("subject_id", courseQuery.getSubjectId());
        }
        if(!StringUtils.isEmpty(courseQuery.getSubjectParentId())) {
            wrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }
        if(!StringUtils.isEmpty(courseQuery.getTeacherId())) {
            wrapper.eq("teacher_id", courseQuery.getTeacherId());
        }
        eduCourseService.page(page, wrapper);
        return R.ok().data("rows", page.getRecords()).data("total", page.getTotal());
    }

    @DeleteMapping("removeCourse/{id}")
    public R removeCourseById(@PathVariable("id") String id) {
        boolean result = eduCourseService.removeCourseByCourseId(id);
        return result ? R.ok() : R.error();
    }

    @GetMapping("getListCourse")
    public R getListCourse() {
        List<EduCourse> eduCourses = eduCourseService.list(null);
        return R.ok().data("list", eduCourses);

    }


}

